export default {
	//日期格式化
	DateFormat(date, fmt) {
		if(!(date instanceof Date)){
			date = new Date(date);
		}
		var o = {
			'M+': date.getMonth() + 1, //月份
			'd+': date.getDate(), //日
			'H+': date.getHours(), //小时
			'm+': date.getMinutes(), //分
			's+': date.getSeconds(), //秒
			'q+': Math.floor((date.getMonth() + 3) / 3), //季度
			S: date.getMilliseconds(), //毫秒
		};
		if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
		for (var k in o) if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));
		return fmt;
	},
	//对象属性名称下划线转驼峰
	LineToHump(object) {
		const obj = {...object}; 
		for (let key in obj) {
			const key2 = key.replace(/_(\w)/g, function(all, letter) {
				return letter.toUpperCase();
			});
			if (key2 !== key) {
				obj[key2] = obj[key];
				delete obj[key];
			}
		}
		return obj;
	},
	// 拼装树
	GetByTree(list) {
		const array = list.filter((e) => e.pid === 0);
		const obj = {};
		list.forEach((e) => {
			obj[e.id] = e;
		});
		list.forEach((e) => {
			if (e.level == 2 && obj[e.pid] != undefined) {
				if (obj[e.pid].children) {
					obj[e.pid].children.push(e);
				} else {
					obj[e.pid].children = [e];
				}
			}
		});

		return array;
	},
	// 拼装树
	GetByMenu(list) {
		const array = list.filter((e) => e.parent_id === 0);
		const obj = {};
		list.forEach((e) => {
			obj[e.id] = e;
		});
		list.forEach((e) => {
			if (e.position == 1 && obj[e.parent_id] != undefined) {
				// console.log('list:',obj[e.parent_id])
				if (obj[e.parent_id].children) {
					obj[e.parent_id].children.push(e);
				} else {
					obj[e.parent_id].children = [e];
				}
			}
		});
		return array;
	},
	getTime(v, type) {
		let time = '';
		const date = type == 'new' ? new Date() : new Date(v);
		const year = date.getFullYear();
		var month = date.getMonth() + 1;
		month = month > 9 ? month : '0' + month;
		var day = date.getDate();
		day = day > 9 ? day : '0' + day;
		var hh = date.getHours();
		hh = hh > 9 ? hh : '0' + hh;
		var mm = date.getMinutes();
		mm = mm > 9 ? mm : '0' + mm;
		var ss = date.getSeconds();
		ss = ss > 9 ? ss : '0' + ss;
		// var ms = date.getMilliseconds();
		if (type == 'new') {
			time = year + '-' + month + '-' + day + ' ' + hh + ':' + mm + ':' + ss;
		} else {
			time = year + '-' + month + '-' + day + '-' + hh + ':' + mm + ':' + ss;
		}
		return time;
	},
};
